const runs = [];
module.exports = (func) => {
    if (!(func instanceof Function)) throw new Error("func must be a function");
    runs.push(func);
}
let _endFlag = false;
const exit = async function(id) {
    if (_endFlag) return;
    _endFlag = true;
    APP.infoLog("PROCESS", "控制面板正在结束与回收资源,请稍等...");

    for (let i in runs) {
        try {
            let func = runs[i];
            if (func.constructor.name === 'AsyncFunction') await func();
            else func();
        } catch (err) {
            APP.error("PROCESS - 无法运行清理钩子", err);
        }
    }
    APP.infoLog("PROCESS", "EXIT...");
    process.exit(isNaN(id) ? 0 : parseInt(id));
};
process.on("SIGINT", exit);
module.exports.exit = exit;